﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Helpers;
using Model;
using System.Collections.Generic;
using System.IO;

namespace FEIStorageSite.Admin
{
    public partial class Files : System.Web.UI.Page
    {
        public List<CategoryItem> categoriesList;

        public void FilesDataPagerPreRender(object sender, EventArgs e)
        {
            if (Request["type"] == null)
            {
                List<FileItem> files = DAL.GetFilesList();

                foreach (FileItem curFileItem in files)
                {
                    try
                    {
                        string filePath = Server.MapPath(curFileItem.Path).Replace("\\admin", "");
                        FileInfo curfileInfo = new FileInfo(filePath);
                        curFileItem.FileSize = curfileInfo.Length.ToString();
                    }
                    catch (Exception exception)
                    {
                        curFileItem.FileSize = "0";
                    }
                }

                if (files.Count != 0)
                {
                    filesListView.DataSource = files;
                    filesListView.DataBind();
                    deleteAllButton.Visible = true;
                }
                else
                {
                    filesPageError.Text = "Поки що немає файлів";
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                DAL.connectionString = "Data Source=" + Server.MapPath("App_Data\\fei_storage.db");
                categoriesList = DAL.GetCategories();
                if (Request["type"] == "file" && !IsPostBack)
                {
                    int fileID = Convert.ToInt32(Request["id"]);
                    FileItem file = DAL.GetFile(fileID);

                    try
                    {
                        string filePath = Server.MapPath(file.Path).Replace("\\admin", "");
                        FileInfo fileInfo = new FileInfo(filePath);
                        file.FileSize = fileInfo.Length.ToString();
                    }
                    catch (Exception exception)
                    {
                        file.FileSize = "0";
                    }                    

                    idLabel.Text = file.ID.ToString();
                    pathTextBox.Text = file.Path;
                    nameTextBox.Text = file.Name;
                    descriptionTextBox.Text = file.Description;
                    ipLabel.Text = file.Ip;
                    dateLabel.Text = file.Date.ToString();
                    categoryListBox.DataSource = categoriesList;
                    categoryListBox.DataBind();
                    categoryListBox.SelectedValue = file.CategoryID.ToString();
                    downCountLabel.Text = file.DownCount.ToString();                    
                    sizeLabel.Text = file.FileSize;
                    fileView.Visible = true;
                }

                switch (Request["action"])
                {
                    case "delete":
                        int fileID = Convert.ToInt32(Request["id"]);
                        FileItem curFile = DAL.GetFile(fileID);
                        string filePath = Server.MapPath(curFile.Path).Replace("\\admin", "");
                        FileInfo fileInfo = new FileInfo(filePath);
                        fileInfo.Delete();
                        DAL.DeleteFile(fileID);
                        Response.Redirect("Files.aspx?action=confirm_delete");
                        break;
                    case "confirm_delete":
                        filesPageError.Text = "Файл успішно видалено";
                        break;
                    case "delete_all":
                        DAL.DeleteAllFiles();

                        DirectoryInfo storageDir = new DirectoryInfo(Server.MapPath("storage/").Replace("\\admin", ""));
                        FileInfo[] filesList = storageDir.GetFiles();

                        foreach (FileInfo curFileInfo in filesList)
                        {
                            curFileInfo.Delete();
                        }
                        
                        Response.Redirect("Files.aspx");
                        break;
                }
            }
            catch (Exception exception)
            {
                filesPageError.Text = exception.Message;
            }
        }

        public void SaveButtonClick(object sender, EventArgs e)
        {
            try
            {
                FileItem file = new FileItem();
                file.ID = Convert.ToInt32(idLabel.Text);
                file.Path = pathTextBox.Text;
                file.Name = nameTextBox.Text;
                file.Description = descriptionTextBox.Text;
                file.Ip = ipLabel.Text;
                file.Date = Convert.ToDateTime(dateLabel.Text);
                file.CategoryID = Convert.ToInt32(categoryListBox.SelectedValue);
                file.DownCount = Convert.ToInt32(downCountLabel.Text);
                DAL.EditFile(file);
                throw new Exception("Зміни успішно збережено");
            }
            catch (Exception exception)
            {
                filesPageError.Text = exception.Message;
            }
        }
    }
}
